package com.leesche.zcsp.node.app.v1.controller.speed.template;

import com.leesche.zcsp.node.app.v1.controller.operate.AppPlaceController;
import com.leesche.zcsp.node.utils.web.PagingResult;
import com.leesche.zcsp.node.utils.web.messge.WebResult;
import com.leesche.zcsp.node.web.model.PubDevTemplate;
import com.leesche.zcsp.node.web.model.PubDevice;
import com.leesche.zcsp.node.web.model.PubPlace;
import com.leesche.zcsp.node.web.model.PubTemplateDevCriteria;
import com.leesche.zcsp.node.web.service.PubDevTemplateService;
import com.leesche.zcsp.node.web.service.PubDeviceService;
import com.leesche.zcsp.node.web.service.PubPlaceService;
import com.leesche.zcsp.node.web.service.PubTemplateDevService;
import com.leesche.zcsp.node.web.service.impl.data.read.*;
import com.leesche.zcsp.node.web.service.privilege.PrivilegeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * Created by dennies yang  on 2017-02-08.
 */
@Controller
@RequestMapping(value = "/app/v1/testSpeed/template")
public class DevTampateContoller {
    private final static Logger LOGGER = LoggerFactory.getLogger(DevTampateContoller.class);

    @Autowired
    private PubDevTemplateService templateService;

    @Autowired
    private PubTemplateDevService templateDevService;

    @Autowired
    PrivilegeService privilegeService;

    /**
     * 模板列表
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/listTemplatesByPage", method = RequestMethod.POST)
    public Map<String,Object> listView(
            Integer page,Integer  rows
    ){
        try {
//            Long adminId = 1L;
            QueryPubDevTemplateCondition condition=new QueryPubDevTemplateCondition();
            Long adminId = privilegeService.getCurrentPrincipalForApp().getUserid();
            condition.setAdminId(adminId);
            condition.setPage(page);
            condition.setRows(rows);
            PagingResult<PubDevTemplateDto> list = templateService.findAllByPage(condition);
            return WebResult.success().put("data",list).toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }


    /**
     * 模板列表
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/listTemplates", method = RequestMethod.POST)
    public Map<String,Object> listView(
    ){
        try {
//            Long adminId = 1L;
            QueryPubDevTemplateCondition condition=new QueryPubDevTemplateCondition();
            Long adminId = privilegeService.getCurrentPrincipalForApp().getUserid();
            condition.setAdminId(adminId);
            List<PubDevTemplateDto> list = templateService.findListsByCondition(condition);
            return WebResult.success().put("data",list).toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 添加模板
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addTemplate", method = RequestMethod.POST)
    public Map<String,Object> add(
            PubDevTemplate template
    ){
        try {
//            Long adminId = 1L;
            Long adminId = privilegeService.getCurrentPrincipalForApp().getUserid();
            template.setAdminid(adminId);
            template.setCreatetime(new Date());
            template.setUpdatetime(new Date());
            templateService.insert(template);
            return WebResult.success().toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }


    /**
     * 修改模板
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editTemplate", method = RequestMethod.POST)
    public Map<String,Object> edit(
            PubDevTemplate template
    ){
        try {
            PubDevTemplate oldTemplate=templateService.selectByPrimaryKey(template.getTemplateid());
            Long adminId = privilegeService.getCurrentPrincipalForApp().getUserid();
            if(oldTemplate.getAdminid()==null||!oldTemplate.getAdminid().equals(adminId)){
                return WebResult.failure("你无权限!").toMap();
            }
            template.setAdminid(adminId);
            template.setCreatetime(new Date());
            template.setUpdatetime(new Date());
            templateService.updateByPrimaryKey(template);
            return WebResult.success().toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }


    /**
     * 删除模板
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteTemplate", method = RequestMethod.POST)
    public Map<String,Object> editDevicePlace(
            Long templateId
    ){
        try {
            PubDevTemplate oldTemplate=templateService.selectByPrimaryKey(templateId);
            Long adminId = privilegeService.getCurrentPrincipalForApp().getUserid();
            if(oldTemplate.getAdminid()==null||!oldTemplate.getAdminid().equals(adminId)){
                return WebResult.failure("你无权限!").toMap();
            };
            PubTemplateDevCriteria criteria=new PubTemplateDevCriteria();
            criteria.or().andDevtemplateidEqualTo(templateId);
            templateDevService.deleteByCriteria(criteria);
            templateService.deleteByPrimaryKey(templateId);
            return WebResult.success().toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }
}
